package com.dotexe.webframework.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.log4j.Logger;

public class Configurator {
	private static Logger logger = null;
	// configuration parameters
	public static String ROOT_USECASE_PACKAGE = "";

	public static final String DEFAULT_CONFIG_FILE_NAME = "dotexe-webframwork.properties";
	static {
		
		logger = Logger.getLogger(Configurator.class);
		
		// try to load config from file
		try {
			InputStream propsInputStream = ApplicationController.class.getClassLoader().getResourceAsStream(DEFAULT_CONFIG_FILE_NAME);
			// if config file is loaded, initialize properties
			if (propsInputStream != null) {
				Properties properties = new Properties();
				properties.load(propsInputStream);

				setRootUsecasePackage(properties.getProperty("BASE_PACKAGE_NAME"));
			}
			
			else {
				logger.warn("Could not find config file [" + DEFAULT_CONFIG_FILE_NAME + "]");
			}
		} catch (IOException ex) {
			System.out.println("[WARNING] Could not read dotexe config file [" + DEFAULT_CONFIG_FILE_NAME + "]");
		}
	}

	public static void setRootUsecasePackage(String rootPackage) {
		// check if package name ends with .
		if (!rootPackage.endsWith(".")) {
			rootPackage = rootPackage.concat(".");
		}
		ROOT_USECASE_PACKAGE = rootPackage;
	}

}
